*This file produce results for Appendix B 
*of "Revealed Preference from Voluntary Contributions" (Bobolink OE)

clear all
set more off
capture log close

*Specify directories here
global dta "C:\Users\zhenshanchen\Documents\RP from VC\Valuing public good WTP\dta"
global results "C:\Users\zhenshanchen\Documents\RP from VC\Valuing public good WTP\Results"

set seed 123456789

 
 ***********************************
 *   OE DH  - Appendix B results   *
 ***********************************
 clear all
 set more off
 use "$dta\reanalysis_hurdle_final0.dta",clear

 drop if dcamt>offeramt&offeramt_all>0&dc==1
 replace offeramt=dcamt if dcamt<offeramt&offeramt_all>0&dc==1
 /*for comparison-revenue*/
 global varlist_y "acres view numboboter NFledglings_recode age female denv mokids round2 Refund07 lncoastdist mis_indiv"
 global varlist_d "retcode lnppower age female denv mokids lncoastdist lnparceldens  mis_indiv"
 

 
 sum $varlist_y
 probit donate $varlist_d if dc==0
 mat list e(b)
 mat bparticipation=e(b)
 qui tobit offeramt_all $varlist_y if dc==0, ll(0)
 mat boutcome= e(b)
 mat list boutcome
 
 program define dhbobo_oe
    version 14
    args lnf beta1 beta2 beta3 beta4 beta5 beta6 beta7
    tempvar d w y p z p0 p1
    quietly gen double `d'=($ML_y1>0)
	quietly gen double `w'=($ML_y1>0&$ML_y1<20)
    quietly gen double `y'=$ML_y1
	quietly gen double `p'=normprob(`beta3')
    quietly gen double `z'=(`y'-`beta1')/(`beta2')
    quietly gen double `p0'=1-(`p'*normprob(-`z'))
    quietly gen double `p1'=(`p'*normalden(`z'))/`beta2'
    quietly replace `lnf'=(1-`d')*ln(`p0')+`d'*ln(`p1')
 end 
 
 mat b=(boutcome,bparticipation)
 ml model lf dhbobo_oe (above: offeramt_all=$varlist_y)(sigma1: )(hurdle: donate=$varlist_d) if dc==0
 ml init b,copy
 ml maximize
 estat ic
 
 eststo ModelDHOE
 esttab ModelDHOE, p star(+ .1 * .05 ** .01 *** .001)
 esttab ModelDHOE using"$results\Table B1.csv", replace se scalars(ll k AIC BIC) star(+ .1 * .05 ** .01 *** .001) 

 *tobit offeramt_all $varlist_y,ll(0)
 *dhreg offeramt_all $varlist_y, hd($varlist_d)
 predict Participant,equation(hurdle)
 gen p_part=normprob(Participant)
 gen P=(Participant>0)
 
 
 capture drop offer_cond
 predict offer_cond ,xb equation(above)
 gen res=offer_cond-offeramt_all if P==1
 hist res if P==1
 *predict U if P==1,u
 *Problem--predicting donors as nonparticipant
 gen offer_hat=offer_cond*p_part
 tab P, sum(offer_hat) 
 tab P dc,sum(offer_hat)
 hist offer_hat if P==1
 egen revenue_hat=sum(offer_hat)
 sum revenue_hat
 



/* This is the code for the DHEU model under OH, derived in Appendix A, for which the convergency is hard to achieve
*************
*  OE DHEU  *
*************
 clear all
 set more off
 use "$dta\reanalysis_hurdle_final0.dta",clear
 drop if dcamt>offeramt&offeramt_all>0&dc==1
 replace offeramt=dcamt if dcamt<offeramt&offeramt_all>0&dc==1
 
 global varlist_y "acres view numboboter NFledglings_recode age female mokids round2 Refund07 lncoastdist mis_indiv"
 global varlist_d "retcode lnppower age denv lnparceldens lncoastdist mis_indiv"
 sum $varlist_y
 probit donate $varlist_d if dc==0
 mat list e(b)
 mat bparticipation=e(b)
 tobit offeramt_all $varlist_y if dc==0, ll(0) 
 mat boutcome= e(b)
 mat list boutcome
 
 program define dhbobo_oe
    version 14
    args lnf beta1 beta2 beta3 beta4 beta5 beta6 beta7
    tempvar d y p z p0 p1
    quietly gen double `d'=($ML_y1>0)
    quietly gen double `y'=$ML_y1
	quietly gen double `p'=normprob(`beta3')
    quietly gen double `z'=(`beta5'*$ML_y1-`beta1'-(`beta4'-`beta5'*normprob(`beta6'+`beta7'*$ML_y1))/(`beta7'*normalden(`beta6'+`beta7'*$ML_y1)))/(`beta2')
    quietly gen double `p0'=1-(`p'*normprob(-`z'))
    quietly gen double `p1'=(`p'*normalden(`z'))/`beta2'
    quietly replace `lnf'=(1-`d')*ln(`p0')+`d'*ln(`p1')
 end 
 
 mat b=(boutcome,bparticipation,.3,.5,.5,.2)
 ml model lf dhbobo_oe (above: offeramt_all=$varlist_y)(sigma1: )(hurdle: donate=$varlist_d)(warm_glow:)(beta:)(mu:)(tao:) if dc==0
 ml init b,copy
 ml maximize
 estat ic
 